
package com.kanjian.music.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.kanjian.music.R;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.fragment.MusicListFragment;
import com.kanjian.music.fragment.MusicianListFragment;

public class SubscriptionActivity extends BaseActivity {

    private Button mBtn_musicList;

    private Button mBtn_musicianList;

    private MusicListFragment mMusicListFragment;

    private MusicianListFragment mMusicianListFragment;

    private ArrayList<Music> mMusicList;

    private ArrayList<Musician> mMusicianList;

    private FragmentTransaction mFragmentTransaction;

    public static final int SUBSCRIPTION_FOCUS_MUSICLIST = 0;

    public static final int SUBSCRIPTION_FOCUS_MUSICIANLIST = 1;

    private int mFocus = SUBSCRIPTION_FOCUS_MUSICLIST;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_rank);
        super.onCreate(arg0);
        initView();
        setTitle();
    }

    private void initView() {
        Bundle bundle = getIntent().getExtras();

        mMusicList = (ArrayList<Music>) bundle.getSerializable(IntentConstants.MUSICLIST);
        mMusicianList = (ArrayList<Musician>) bundle.getSerializable(IntentConstants.MUSICIANLIST);
        mFocus = bundle.getInt(IntentConstants.SUBSCRIPTION_FOCUS);
        mMusicListFragment = MusicListFragment.newInstance(mMusicList);
        mMusicianListFragment = MusicianListFragment.newInstance(mMusicianList);

        mBtn_musicList = (Button) findViewById(R.id.rank_btn_music);
        mBtn_musicianList = (Button) findViewById(R.id.rank_btn_musician);
        mBtn_musicList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleFragment(mMusicListFragment, mMusicianListFragment);
            }
        });
        mBtn_musicianList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleFragment(mMusicianListFragment, mMusicListFragment);
            }
        });

        if (mFocus == SUBSCRIPTION_FOCUS_MUSICLIST) {
            toggleFragment(mMusicListFragment, mMusicianListFragment);
        } else {
            toggleFragment(mMusicianListFragment, mMusicListFragment);
        }

    }

    private void toggleFragment(Fragment showFragment, Fragment hideFragment) {
        mFragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (hideFragment != null && hideFragment.isAdded() && !hideFragment.isHidden()) {
            mFragmentTransaction.hide(hideFragment);
        }
        if (showFragment != null) {
            if (!showFragment.isAdded()) {
                mFragmentTransaction.add(R.id.rank_fragment_container, showFragment);
            } else {
                mFragmentTransaction.show(showFragment);
            }
        }
        mFragmentTransaction.commitAllowingStateLoss();
    }

    private void setTitle() {
        super.setTitle("我的订阅");
    }

}
